/**
* @program: src
*
* @description:用户登录注册接口
*
* @author: Mr.chen
*
* @create: 2020-12-19 10:56
**/
package group

import (
	proto "ydd-im/service/group/proto"
	"ydd-im/service/api/define"
	dbcli "ydd-im/service/dbproxy/handle"
	"ydd-im/util/logger"
	"ydd-im/util/response"
	"context"
	"encoding/json"
	"go.uber.org/zap"

)

// User : 用于实现UserServiceHandler接口的对象
type Controller struct {
}




// Creategroup : 处理用户创建群请求
func (u *Controller) Creategroup(ctx context.Context, req *proto.CreategroupReq) (res *proto.CreategroupResp,err error) {
	res = &proto.CreategroupResp{} // 返回是指针，必须初始化，否则报错panic
	dbResp, err := dbcli.CreateGroupIsCluster(int(req.Userid), int(req.Sid), req.Id,req.Name,req.Members)
	if err != nil { // 返回是指针，必须是指针，否则报错panic
		logger.GetLg().Error("创建群失败",
			zap.String("host", define.LocalHost),
			zap.String("port", define.Port),
			zap.Any("Userid",req.Userid ),
			zap.Any("Sid",req.Sid ),
			zap.String("Id",req.Id),
			zap.String("Name",req.Name ),
			zap.String("Members",req.Members ),
			zap.Any("err", err.Error()),
		)
		res.Code = response.ErrorCode
		res.Msg = err.Error()
		return res,nil
	}
	data, err := json.Marshal(dbResp.Data)
	logger.GetLg().Info("创建群接口返回结果",
		zap.String("host", define.LocalHost),
		zap.String("port", define.Port),
		zap.Any("Userid",req.Userid ),
		zap.Any("Sid",req.Sid ),
		zap.String("Id",req.Id),
		zap.String("Name",req.Name ),
		zap.String("Members",req.Members ),
		zap.Int("Code",dbResp.Code ),
		zap.String("Msg",dbResp.Msg ),
		zap.Any("firstdata",dbResp.Data),
		zap.Any("data",data),
	)
	if err != nil {
		res.Code = response.ErrorCode
		res.Msg = err.Error()
		return res,nil
	}
	res.Code = int64(dbResp.Code)
	res.Msg = dbResp.Msg
	res.Data = data
	return res,nil
}

// JoinGroup : 处理用户加入群请求
func (u *Controller) JoinGroup(ctx context.Context, req *proto.JoinGroupReq) (res *proto.JoinGroupResp,err error) {
	res = &proto.JoinGroupResp{} // 返回是指针，必须初始化，否则报错panic
	dbResp, err := dbcli.JoinGroupIsCluster(int(req.Userid), int(req.Sid), req.Id,req.Members)
	if err != nil { // 返回是指针，必须是指针，否则报错panic
		logger.GetLg().Error("创建群失败",
			zap.String("host", define.LocalHost),
			zap.String("port", define.Port),
			zap.Any("Userid",req.Userid ),
			zap.Any("Sid",req.Sid ),
			zap.String("Id",req.Id),
			zap.String("Members",req.Members ),
			zap.Any("err", err.Error()),
		)
		res.Code = response.ErrorCode
		res.Msg = err.Error()
		return res,nil
	}
	logger.GetLg().Info("创建群接口返回结果",
		zap.String("host", define.LocalHost),
		zap.String("port", define.Port),
		zap.Any("Userid",req.Userid ),
		zap.Any("Sid",req.Sid ),
		zap.String("Id",req.Id),
		zap.String("Members",req.Members ),
		zap.Int("Code",dbResp.Code ),
		zap.String("Msg",dbResp.Msg ),
		zap.Any("firstdata",dbResp.Data),

	)
	if err != nil {
		res.Code = response.ErrorCode
		res.Msg = err.Error()
		return res,nil
	}
	res.Code = int64(dbResp.Code)
	res.Msg = dbResp.Msg
	return res,nil
}